package com.example.demo.service.impl;

import com.example.demo.entity.Books;
import com.example.demo.service.MyService;

import com.mongodb.client.MongoClient;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @description:
 * @author: ly
 * @create: 14-06-28 14:31:17
 */
@Slf4j
@Service
public class MyServiceImpl implements MyService {

    @Autowired
    MongoTemplate mongoTemplate;

    @Autowired
    Query query;

    @Override
    public List<Books> getAllBooks() {

        mongoTemplate.find(new Query(), Books.class, "books");

        return mongoTemplate.findAll(Books.class);

    }


    @Override
    public Books getBookById(String name){

        System.out.println(name);

        Query query = new Query(Criteria.where("name").is(name));

        Books books = mongoTemplate.findOne(query, Books.class);

        System.out.println(books);

//        MongoClient mongoClient  = new MongoClient("127.0.0.1",27017);
//
//        MongoDatabase mydb = mongoClient.getDatabase("mydb");
//
//        MongoCollection<Document> books = mydb.getCollection("books");

//        log.info(""+books);

        return books;
    }

    @Override
    public List<Books> getBooksByName(String name) {
        query.addCriteria(Criteria.where("name").regex(name));
        List<Books> books = mongoTemplate.find(query, Books.class, "books");
        return books;
    }

    @Override
    public List<Books> getBooksBySort(String condition) {
        if("DESC".equalsIgnoreCase(condition)){



        }else {

        }
        return null;
    }
}
